$(document).ready(function(){
    $('#createPostForm').ajaxForm({
        success: function(){
            // Clear the posted message
            $('#texPostContent').val('');
            $('#texPostContent').text('');
            // TODO : Show something to the user to say his post is created
            loadPosts();
        }
    });
    loadPosts();
    $('.ml-microblog-post-read-more .ml-microblog-link').live('click',function(e){
        e.preventDefault();
        // FIXME : Doesn't work in IE6-7, probably caused by href attribute
        $('#postsContainer').load('?r=ml-microblog/post/show', {id: $(this).attr('href').substr('post-'.length + 1)});
    });
    $('.ml-microblog-link-home').live('click',function(e){
        e.preventDefault();
        loadPosts();
    });
    $('.ml-microblog-tag-link').live('click', function(e){
        e.preventDefault();
        $('#postsContainer').load('?r=ml-microblog/tag/show', {tag: $(this).attr('href')});
    });
    $('.ml-pager-item').live('click', function(e){
        e.preventDefault();
        $('#postsContainer').load($(this).attr('href'), function(){
           $(document).scrollTop(0);
        });
    });
    $('#signup').colorbox({title:strSignup});
    $('#signin').colorbox({title:strSignin});
    $('#createPost').colorbox();
});

/**
 * Ajax loading of all posts
 */
function loadPosts(){
    $.ajax({
        url: "?r=ml-microblog/post/load",
        success: function(data){
            $('#postsContainer').html(data);
        }
    });
}
